package com.commerce.reader.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.commerce.reader.Entity.Book;
import com.commerce.reader.Entity.Chapter;
import com.commerce.reader.R;

import org.litepal.LitePal;

import java.util.List;

public class MyCollectionAdapter extends BaseAdapter {

    Context context; List<Book> book;

    public MyCollectionAdapter(Context context, List<Book> book) {
        this.context=context;
        this.book= book;
    }

    @Override
    public int getCount() {
        return book.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder{
        private TextView textt;
        private TextView texth;
    }

    @Override
    public View getView(int i, View view1, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view1==null){
            view1 = LayoutInflater.from(context).inflate(R.layout.laoyout_collection,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.textt=view1.findViewById(R.id.lc_title);
            viewHolder.texth=view1.findViewById(R.id.lc_history);
            view1.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) view1.getTag();
        }
        viewHolder.textt.setText(book.get(i).getBook());
        String idName = book.get(i).getBook() + book.get(i).getName();
        long start = System.currentTimeMillis();
        List<Chapter> chapters = LitePal.where("idName = ?", idName)
                .find(Chapter.class);
        long end = System.currentTimeMillis();
        Log.d("TAG", "getView: " + (end - start) + "-------"+ book.size() + "-------"+ i);
        String chapter = chapters.get(book.get(i).getIds()).getChapter();
        viewHolder.texth.setText(chapter);
        return view1;
    }
}
